﻿/*
 * Copyright (C) 2013 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using UnityEngine;
using System.Collections;
using UnityEngine.SocialPlatforms;

public class MainMenu : MonoBehaviour {
    public GUISkin GuiSkin; // set by editor
    public GUIStyle NormalButtonStyle; // set by editor
    public GUIStyle DisabledButtonStyle; // set by editor
    public GUIStyle SignInButtonStyle; // set by editor

    void OnGUI() {
        GUI.skin = GuiSkin;
        if (GameManager.Instance.Mode != GameManager.GameMode.MainMenu) return;

        Gu.Label(Gu.Center(-300), Gu.Middle(-350), Gu.Dim(80), "Outpost Defender");

        int y = -150;
        bool signedIn = Social.localUser.authenticated;

        if (Gu.Button(Gu.Center(-300), Gu.Middle(y), Gu.Dim(600), Gu.Dim(150), Gu.Dim(50), "Play!")) {
            GameManager.Instance.StartPlaying();
        }

        y += 170;
        if (Gu.Button(Gu.Center(-300), Gu.Middle(y), Gu.Dim(600), Gu.Dim(150), Gu.Dim(50), "View Achievements",
            signedIn ? NormalButtonStyle : DisabledButtonStyle) && signedIn) {
            Social.Active.ShowAchievementsUI();
        }

        y += 170;
        if (Gu.Button(Gu.Center(-300), Gu.Middle(y), Gu.Dim(600), Gu.Dim(150), Gu.Dim(50), "View Leaderboards",
            signedIn ? NormalButtonStyle : DisabledButtonStyle) && signedIn) {
            Social.Active.ShowLeaderboardUI();
        }

        #if UNITY_ANDROID
        if (!signedIn && Gu.Button(Gu.Left(20), Gu.Bottom(145), Gu.Dim(500), Gu.Dim(125), Gu.Dim(40),
                    "Sign in with Google", SignInButtonStyle)) {
            SignInWithGoogle();
        }
        if (signedIn) {
            Gu.Label(Gu.Left(30), Gu.Bottom(100), Gu.Dim(40), "You are signed in with Google.");
        }
        #endif
    }

    void SignInWithGoogle() {
        Debug.Log("About to instantiate PlayGameServicesPlatform...");
        ISocialPlatform isoc = new PlayGameServicesPlatform();
        Debug.Log("Instantiated PlayGameServicesPlatform. Setting Social.Active.");
        Social.Active = isoc;
        Debug.Log("Attempting authentication.");
        isoc.localUser.Authenticate(success => {
            Debug.Log("*** Authentication result: " + success);
        });
    }
}
